using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.SqlClient;
namespace ServerApi.Models.Menu
{
    public class Role
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string PageName { get; set; }
        public int TypeRoleId { get; set; }
        public int Status { get; set; }


        public static string COLUMN_ID = "Id";
        public static string COLUMN_NAME = "Name";
        public static string COLUMN_PAGENAME = "PageName";
        public static string COLUMN_TYPEROLEID = "TypeRoleId";
        public static string COLUMN_STATUS = "Status";


        public static string TableName = "Role";
        public Role()
        {
            this.Id = 0;
            this.Name = string.Empty;
            this.PageName = string.Empty;
            this.TypeRoleId = 0;
            this.Status = 0;

        }
    }
    public class RoleModel : BaseModel<Role>
    {
        public RoleModel()
        {
            table = Role.TableName;
        }
        public bool Insert(Role obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter(Role.COLUMN_NAME,obj.Name),
					new SqlParameter(Role.COLUMN_PAGENAME,obj.PageName),
					new SqlParameter(Role.COLUMN_TYPEROLEID,obj.TypeRoleId),
					new SqlParameter(Role.COLUMN_STATUS,obj.Status),
                    
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Update(Role obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter(Role.COLUMN_NAME,obj.Name),
					new SqlParameter(Role.COLUMN_PAGENAME,obj.PageName),
					new SqlParameter(Role.COLUMN_TYPEROLEID,obj.TypeRoleId),
					new SqlParameter(Role.COLUMN_STATUS,obj.Status),

                }, new SqlParameter[]{
					new SqlParameter(Role.COLUMN_ID,obj.Id),
																				 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter(Role.COLUMN_ID,Id),
																				 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<Role> GetByPage(int page, int pagesize, List<Role> source)
        {
            List<Role> list = new List<Role>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<Role> GetListBy(DataTable dt)
        {
            List<Role> list = new List<Role>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Role obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public Role GetBy(DataRow dr)
        {
            try
            {
                Role obj = new Role();
                obj.Id = (dr[Role.COLUMN_ID] is DBNull) ? 0 : int.Parse(dr[Role.COLUMN_ID].ToString());
                obj.Name = (dr[Role.COLUMN_NAME] is DBNull) ? string.Empty : dr[Role.COLUMN_NAME].ToString();
                obj.PageName = (dr[Role.COLUMN_PAGENAME] is DBNull) ? string.Empty : dr[Role.COLUMN_PAGENAME].ToString();
                obj.TypeRoleId = (dr[Role.COLUMN_TYPEROLEID] is DBNull) ? 0 : int.Parse(dr[Role.COLUMN_TYPEROLEID].ToString());
                obj.Status = (dr[Role.COLUMN_STATUS] is DBNull) ? 0 : int.Parse(dr[Role.COLUMN_STATUS].ToString());

                return obj;
            }
            catch (Exception)
            {
            }

            return new Role();
        }
        public Role GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new Role();
        }
        public Role GetByID(int Id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter(Role.COLUMN_ID,Id),
																				
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<Role> getListRoleBy(int AccId)
        {
            string query = "select a.* from " + table + " a," + RoleUser.TableName + " b";
            query += " where a.id=b.roleid and b.userid=" + AccId;
            DataTable dt = DB.ExecuteDataTable(query, null);
            return GetListBy(dt);
        }
    }
}
